<?php

namespace Admin\Controller;

class ProjectController extends AdminController
{
    public function index()
    {
        $this->display("index");
    }

    public function view()
    {
        $n = D("Project");
        import("ORG.Util.Page"); //导入分页类
        $page = $_POST["page"];
        $rows = $_POST["rows"];
        $sort = $_POST["sort"] ? $_POST["sort"] : 'id';
        $order = $_POST["order"] ? $_POST["order"] : 'desc';
        $count = $n->count(); //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->
        $list = $n->relation(true)->page($page, $rows)->order(array($sort => $order))->select();
        //$list = $n->select();
        foreach ($list as $key => $value) {
            $diseaseid_arr = explode(',', $value['diseaseids']);
            $mediatypeid_arr = explode(',', $value['mediatypeid']);
            $mediaid_arr = explode(',', $value['mediaid']);
            $userid_arr = explode(',', $value['userids']);

            $disease = M('Disease');
            $con['id'] = array('in', $diseaseid_arr);
            $diseaselist = $disease->where($con)->select();
            $list[$key]['diseases'] = $diseaselist;

            $mediatype = M('mediatype');
            $con['id'] = array('in', $mediatypeid_arr);
            $mediatypelist = $mediatype->where($con)->select();
            $list[$key]['mediatypelist'] = $mediatypelist;

            $media = M('media');
            $con['id'] = array('in', $mediaid_arr);
            $medialist = $media->where($con)->select();
            $list[$key]['medialist'] = $medialist;

            $media = M('user');
            $con['id'] = array('in', $userid_arr);
            $medialist = $media->where($con)->select();
            $list[$key]['users'] = $medialist;
        }
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        echo($result); //输出json数据
    }

    public function viewlist()
    {
        $n = D("Project");
        import("ORG.Util.Page"); //导入分页类
        $page = $_POST["page"];
        $rows = $_POST["rows"];
        $sort = $_POST["sort"] ? $_POST["sort"] : 'hospitalid';
        $order = $_POST["order"] ? $_POST["order"] : 'asc';
        $count = $n->count(); //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->
        $list = $n->relation(true)->page($page, $rows)->order(array($sort => $order))->select();
        //$list = $n->select();
        foreach ($list as $key => $value) {
            $diseaseid_arr = explode(',', $value['diseaseids']);
            $mediatypeid_arr = explode(',', $value['mediatypeid']);
            $mediaid_arr = explode(',', $value['mediaid']);
            $userid_arr = explode(',', $value['userids']);

            $disease = M('Disease');
            $con['id'] = array('in', $diseaseid_arr);
            $diseaselist = $disease->where($con)->select();
            $list[$key]['diseases'] = $diseaselist;

            $mediatype = M('mediatype');
            $con['id'] = array('in', $mediatypeid_arr);
            $mediatypelist = $mediatype->where($con)->select();
            $list[$key]['mediatypelist'] = $mediatypelist;

            $media = M('media');
            $con['id'] = array('in', $mediaid_arr);
            $medialist = $media->where($con)->select();
            $list[$key]['medialist'] = $medialist;

            $media = M('user');
            $con['id'] = array('in', $userid_arr);
            $medialist = $media->where($con)->select();
            $list[$key]['users'] = $medialist;
        }
        $list = json_encode($list);
        $result = '{"total":' . $count . ',"rows":' . $list . '}';
        return ($result); //输出json数据
    }

    //项目信息公共组件
    public function viewWidget()
    {
        $n = M("Project");
        import("ORG.Util.Page"); //导入分页类
        $count = $n->count(); //计算总数
        //$p = new Page($count,$rows);order($sort+','+$order)->
        //////////////////////判断当前登陆人员所属项目组，并且只查询所属项目的信息
        $projects = session('user.projects');
        foreach ($projects as $key => $value) {
            $projectids[] = $value['id'];
        }
        $condition['id'] = array('in', $projectids);
        $condition['status'] = 1;
        //dump($condition);
        //////////////////////////////////////////////////////////////////
        $list = $n->where($condition)->order('id desc')->select();
        //$list = $n->select();
        $list = json_encode($list);
        //$result = '{"total":'.$count.',"rows":' . $list . '}';
        echo($list); //输出json数据
    }

    //添加提交处理
    public function add()
    {
        if (empty($_POST)) {
            $Hospital = A('Hospital');
            $hospitallist = $Hospital->viewlist();
            $this->assign('hospitallist', $hospitallist);
            $Mediatype = A('Mediatype');
            $mediatypelist = $Mediatype->viewlist();
            $this->assign('mediatypelist', $mediatypelist);
            $Disease = A('Disease');
            $diseaselist = $Disease->viewlist();
            $this->assign('diseaselist', $diseaselist);
            $User = A('User');
            $userlist = $User->viewlist();
            $this->assign('userlist', $userlist);
            $this->display();
        } else {
            $m = D("Project");
            $mediaid = I('mediaid');
            $con_temp['id'] = array('in', $mediaid);
            $mediaid = implode(',', $mediaid);
            $mediatypeid = M('Media')->distinct(true)->where($con_temp)->getField('type', true);
            $_POST['mediatypeid'] = implode(',', $mediatypeid);
            $_POST['mediaid'] = $mediaid;
            if (!$vo = $m->create()) {
                $this->error($m->getError(), 3);
            } else {
                if ($result = $m->add($vo)) {
                    $this->success('添加成功！', 1);
                } else {
                    $this->error('添加失败！', 0);
                }
            }
        }
    }

    // 删除数据
    public function delete()
    {
        if (!empty($_GET['ids'])) {
            $d = D("Project");
            $condition['id'] = array('in', $_GET['ids']);
            $result = $d->where($condition)->delete();
            if (false !== $result) {
                $this->success('删除成功！', 1);
            } else {
                $this->error('删除出错！', 1);
            }
        } else {
            $this->error('删除项不存在！', 1);
        }
    }

    // 更新数据
    public function edit()
    {
        if (empty($_POST)) {
            $Hospital = A('Hospital');
            $hospitallist = $Hospital->viewlist();
            $this->assign('hospitallist', $hospitallist);
            $Mediatype = A('Mediatype');
            $mediatypelist = $Mediatype->viewlist();
            $this->assign('mediatypelist', $mediatypelist);
            $Media = A('Media');
            $medialist = $Media->viewlist();
            $this->assign('medialist', $medialist);
            $Disease = A('Disease');
            $diseaselist = $Disease->viewlist();
            $this->assign('diseaselist', $diseaselist);
            $User = A('User');
            $userlist = $User->viewlist();
            $this->assign('userlist', $userlist);
            $this->display();
        } else {
            $Form = D("Project");
            $mediaid = I('mediaid');
            $con_temp['id'] = array('in', $mediaid);
            $mediaid = implode(',', $mediaid);
            $mediatypeid = M('Media')->distinct(true)->where($con_temp)->getField('type', true);
            $_POST['mediatypeid'] = implode(',', $mediatypeid);
            $_POST['mediaid'] = $mediaid;
            if ($vo = $Form->create()) {
                $list = $Form->save($vo);
                if ($list !== false) {
                    $this->success('更新成功！', 1);
                } else {
                    $this->error('没有更新任何数据!', 0);
                }
            } else {
                $this->error($Form->getError(), 3);
            }
        }
    }

    public function viewWidgetIndex()
    {
        $projects = session('user.projects');
        foreach ($projects as $key => $value) {
            $projectids[] = $value['id'];
        }
        $n = M("Project");
        //////////////////////判断当前登陆人员所属项目组，并且只查询所属项目的信息
        $condition['id'] = array('in', $projectids);
        //dump($condition);
        //////////////////////////////////////////////////////////////////
        $list = $n->where($condition)->order('addtime asc')->select();
        //$list = $n->select();
        $list = json_encode($list);
        //$result = '{"total":'.$count.',"rows":' . $list . '}';
        return ($list); //输出json数据
    }
}
